<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>

<body>
<div style="margin:0px auto; width: 500px">
    <table border="2px" width="500px">
        <tr>
            <td colspan="6" align="center" style="font-size: 30px">添加联系人</td>
        </tr>


        <tr>
            <td align="left">姓名:</td>
            <td><input type="text" name="cname" id="cname" /></td>
        </tr>



        <tr>
            <td align="left">手机:</td>
            <td><input type="text" name="moblie" id="moblie" /></td>
        </tr>

        <tr>
            <td align="left">QQ：</td>
            <td><input type="text" name="qq" id="qq" /></td>
        </tr>

        <tr>
            <td align="left">Email：</td>
            <td><input type="text" name="email" id="email" /></td>
        </tr>


        <tr>
            <td align="left">地址：</td>
            <td><input type="text" name="address" id="address" /></td>
        </tr>

        <tr>
            <td colspan="2" align="center"><input type="submit" value="保存"
                                                  id="sbt" /> <input type="button" value="重置" /></td>

        </tr>
    </table>


</div>

</body>
</html>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $(function() {

        $("#sbt")
            .click(
                function() {
                    var cname = document.getElementById("cname").value;
                    if (cname == null || cname == "") {
                        alert("姓名不能为空");
                        return false;
                    }
                    var moblie = document.getElementById("moblie").value;
                    if (isNaN(moblie) && moblie % 1 === 0
                        || moblie.length < 11 || moblie == "") {
                        alert("手机号是不能小于11位的整数数字");
                        return false;
                    }

                    var qq = document.getElementById("qq").value;
                    if (isNaN(qq) && qq % 1 == 0) {
                        alert("qq号必须是整数数字");
                        return false;
                    }

                    var email = document.getElementById("email").value;
                    if (email == "") {
                        alert("邮箱格式不正确");
                        return false;
                    }
                    var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                    if (!pattern.test(email)) {
                        alert("邮箱格式不正确");
                        return false;
                    }
                    var cname = $("#cname").val();
                    var qq = $("#qq").val();
                    var address = $("#address").val();
                    var email = $("#email").val();
                    var moblie = $("#moblie").val();



                    $.ajax({
                        url : "${pageContext.request.contextPath}/add",
                        data : {
                            cname : cname,
                            moblie : moblie,
                            qq : qq,
                            email : email,
                            address : address
                        },
                        method : "post",
                        success : function(count) {
                            if (count > 0) {
                                alert("修改成功！！！");
                                location.href = "index.jsp";
                            } else {
                                alert("修改失败！！！");
                                location.href = "index.jsp";
                            }
                        }

                    });
                });

    });
</script>